package com.tony.demo.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.tony.demo.R;
import com.tony.demo.capture.CropActivity;
import com.tony.demo.compressimage.CompressImageActivity;
import com.tony.demo.edittext.EditTextActivity;
import com.tony.demo.fragment.FragmentActivity1;
import com.tony.demo.fragment2.FragmentActivity2;
import com.tony.demo.fragmentdialog.DialogFragmentActivity;
import com.tony.demo.gifplayer.GifplayerActivity;
import com.tony.demo.logic.AsyncLogic;
import com.tony.demo.micrecord.MicRecordActivity;
import com.tony.demo.orientation.OrientationActivity;
import com.tony.demo.pinnedlistview.PinnedHeaderUseCaseActivity;
import com.tony.demo.slidingstage.SlidingStageActivity;

public class MainActivity extends BaseActivity {
	private ListView mListView;
	private IntentArray mIntents;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main_activity);
		mListView = (ListView) findViewById(R.id.lv_main_list);

		initIntents();

		mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mIntents.getNames()));
		mListView.setOnItemClickListener(mItemClickListener);
		
		AsyncLogic.getLogic().sendMessage(AsyncLogic.ACTION_ONE, getBaseContext());
	}

	private OnItemClickListener mItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg3) {
			startActivity(mIntents.getIntents().get(position));
		}
	};

	private void initIntents() {
		mIntents = new IntentArray();

		mIntents.addIntent(new Intent(this, DialogFragmentActivity.class), "DialogFragment");
		mIntents.addIntent(new Intent(this, FragmentActivity1.class), "Fragment1");
		mIntents.addIntent(new Intent(this, FragmentActivity2.class), "Fragment2");
		mIntents.addIntent(new Intent(this, OrientationActivity.class), "Orientation");
		mIntents.addIntent(new Intent(this, EditTextActivity.class), "TouchEditText");
		mIntents.addIntent(new Intent(this, SlidingStageActivity.class), "SlidingStage");
		mIntents.addIntent(new Intent(this, CompressImageActivity.class), "CompressImage");
		mIntents.addIntent(new Intent(this, CropActivity.class), "CaptureView");
		mIntents.addIntent(new Intent(this, PinnedHeaderUseCaseActivity.class), "PinnedHeaderUseCase");
		mIntents.addIntent(new Intent(this, GifplayerActivity.class), "GifPlayer");
		mIntents.addIntent(new Intent(this, MicRecordActivity.class), "MicRecord");
	}

	private class IntentArray {
		private ArrayList<Intent> intents;
		private ArrayList<String> names;

		public void addIntent(Intent intent, String name) {
			if (intents == null) {
				intents = new ArrayList<Intent>(16);
				names = new ArrayList<String>(16);
			}

			if (!intents.contains(intent)) {
				intents.add(intent);
				names.add(name);
			}
		}

		public ArrayList<Intent> getIntents() {
			if (intents == null) {
				intents = new ArrayList<Intent>(1);
			}
			return intents;
		}

		public ArrayList<String> getNames() {
			if (names == null) {
				names = new ArrayList<String>(1);
			}
			return names;
		}
	}
}
